<?php
/**
 *  NevuxBB - Free Forum Software
 *  Copyright (C) 2008, 2010 NevuxBB (No one specific author)
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Forum Index
 * File: index.php
 * Purpose: For showing all the forums
 * Todo: Finish writing it
 *
 */

// load require files
require('system/files/global.php');
include(BASE_PATH . '/system/classes/lite/langcore_lite_class.php');
include(BASE_PATH . '/system/functions/excerpt_string.php');

// start the initalisation of the main page (forum page)
$core_template_system->setvar(array('page_title' => 'Main Index'));

// show the header loaded
$core_template_system->pparse();

// start cache cycle
if(!CacheEngine::isCacheOn() || (CacheEngine::CacheExist('index-forums-loop', 300) === false || CacheEngine::CacheExist('index-forums-class', 300) === false)) {
	// retrieve the forums and categories from the database
	include(BASE_PATH . '/system/classes/generateforums_class.php');
	
	// get the data
	$forums = new generate_forums;
	$forums->set_data('0');
	$forums->get_forum_data();
	$forums->get_stats_array();
	$forums->get_latest_posts_array();
	$loop = $forums->generate_forum_list();
	
	// add to cache if enabled
	if(CacheEngine::isCacheOn()) {
		// pass to cache engine
		CacheEngine::addCache('index-forums-loop', $loop);
		CacheEngine::addCache('index-forums-class', $forums);
	}
} else {

	include(BASE_PATH . '/system/classes/lite/generateforums_lite_class.php');
	
	$forum_loop_cache = CacheEngine::fetchCache('index-forums-loop');
	$forum_main_class = CacheEngine::fetchCache('index-forums-class');

	// load the cached document
	$loop = $forum_loop_cache;
	$forums = $forum_main_class;
}

// start the forums loop
$core_template_system->setloop('loop', $loop);

// define some language variables and urls
$core_template_system->setvar(
array('forum_stats'         => $forums->get_lang_var('FORUM_STATS', 3),
	  'whos_online'         => $forums->get_lang_var('WHOS_ONLINE', 3),
	  'username'            => $core_lang_system->get_var('USERNAME', 4),
	  'password'            => $core_lang_system->get_var('PASSWORD', 4),
	  'rem_me'              => $core_lang_system->get_var('REMEMBER_ME', 4),
	  'nav'                 => $core_lang_system->get_var('NAVIGATION', 4),
	  'ucontrols'		    => $core_lang_system->get_var('USER_CONTROLS', 4),
	  'sf_login'            => $forums->get_lang_var('FORUM_LOGIN_SIDE', 3),
	  'sf_recent_posts'     => $forums->get_lang_var('RECENT_POSTS_SIDE', 3),
	  'sf_no_rposts'        => $forums->get_lang_var('NO_RECENT_POSTS', 3),
	  'online_now_stats'    => sprintf($forums->get_lang_var('ONLINE_NOW', 3), $online_stats['stats']['online_guests'], $online_stats['stats']['online_members'], $online_stats['stats']['online_bots']),
	  'total_forums'        => $forums->total_forums,
	  'lpass_location'      => urlSystem::navParts(7),
	  'w_forum' 			=> $forums->get_lang_var('FORUM', 3),
	  'w_l_post' 			=> $forums->get_lang_var('L_POST', 3),
	  'w_topics_posts' 	    => $forums->get_lang_var('TOPICS_POSTS', 3),
	  'service_information' => $core_lang_system->get_var('SERVICE_I', 4),
	  'no_forums_available' => $forums->get_lang_var('NO_FORUMS_A', 3)));

// statistics
$total_members = $forums->get_local_statistics(1);
$latest_member = explode(',', $forums->get_local_statistics(2));
$total_online_ever = ForumSettings::getTotalOnlineEver();

// build up a large variable array
$core_template_system->setvar(
array('total_threads'    => number_format($forums->get_local_statistics(3)),
      'threads_word'     => $core_lang_system->get_var('THREADS', 4),
	  'posts_word'       => $core_lang_system->get_var('POSTS', 4),
	  'total_posts'      => number_format($forums->get_local_statistics(4)),
	  'total_members'    => number_format($total_members['tm']),
	  'a_newest_member'  => '<a href="' . urlSystem::viewURL(2, array('user_id' => $latest_member[1], 'user_name' => $latest_member[0])) . '" class="whos_online_item">' . $latest_member[0] . '</a>',
	  'newest_member'    => $core_lang_system->get_var('NEWEST_MEMBER', 4),
	  'total_registered' => $core_lang_system->get_var('TOTAL_REGISTERED', 4),
	  'most_online'      => sprintf($forums->get_lang_var('EVER_ONLINE', 3), $total_online_ever['date'], $total_online_ever['total']),
	  'breadcrumb'       => $core_nav_system->generateNav(array('settings' => array('level_down' => false,
																					'main_class' => 'menu'),
																					'information' => array('home' => array('data' => array(1 => 'Home', 2 => 1)))))));


// how many members are currently online?
if($online_stats['stats']['online_members'] == 0 && $online_stats['stats']['online_bots'] == 0) {
	// no one is online, only guests or bots
	$core_template_system->setvar(array('online_members'    => 'No members are currently online...',
										'no_members_online' => true));
} else {
	// loop through the actual members
	$core_template_system->setloop('online_members', $online_stats['online_list']);
}

// perform sidebar recent posts loop
if(!CacheEngine::isCacheOn() || CacheEngine::CacheExist('sidebar-forums-recentposts') === false) {
	// doesn't exist or cache is off, regenerate it
	$query = "SELECT `post_id`, `topic_id`, `post_title`, `text`, `user_name`, `user_id` FROM `nbb_posts` JOIN `nbb_posts_text` ON `nbb_posts`.`post_id` = `nbb_posts_text`.`post_text_id` JOIN `nbb_users` ON `nbb_posts`.`post_user_id` = `nbb_users`.`user_id` ORDER BY `post_id` DESC LIMIT 5;";
	
	// perform query
	$result = Db_Abstraction::query($query);
	
	// define new array
	$sr_posts = array();
	
	// loop through and build array
	while($row = Db_Abstraction::fetch_assoc($result)) {
		// begin loop
		$sr_posts[] = array('post_id' 		=> $row['post_id'],
							'topic_id' 		=> $row['topic_id'],
							'topic_title' 	=> excerpt_text(strip_tags($row['post_title']), 23),
							'post_text'		=> excerpt_text(strip_tags($row['text']), 70),
							'user_name' 	=> $row['user_name'],
							'user_id' 		=> $row['user_id'],
							'user_profile' 	=> urlSystem::viewURL(2, array('user_id'   => $row['user_id'],
																		   'user_name' => $row['user_name'])),
							'topic_url' 	=> urlSystem::viewURL(3, array('topic_id'  => $row['topic_id'])),
							'w_by_r' 		=> $core_lang_system->get_var('BY', 4));
	}
	
	// set new array as loop
	$core_template_system->setvar('recent_posts_sidebar_count', count($sr_posts));
	$core_template_system->setloop('recent_posts_sidebar', $sr_posts);
	
	// add to cache if enabled
	if(CacheEngine::isCacheOn()) {
		// pass to cache engine
		CacheEngine::addCache('sidebar-forums-recentposts', $sr_posts);
	}
} else {
	// load from cache instead
	$core_template_system->setvar('recent_posts_sidebar_count', count(CacheEngine::fetchCache('sidebar-forums-recentposts')));
	$core_template_system->setloop('recent_posts_sidebar', CacheEngine::fetchCache('sidebar-forums-recentposts'));
}

// render the page
$core_template_system->newTemplate(BASE_PATH . '/templates/' . ForumSettings::getCurrentTemplate() . '/forums_index.html');
$core_template_system->pparse();

// unset variables
unset($forums, $loop);
$core_template_system->clearLoops();

require(BASE_PATH . '/system/files/footer.php');
?>